package team3.tracking.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

import android.os.Environment;

public class SDCard {
	boolean available = false;
	boolean writeable = false;
	File file;
	
	public SDCard (String packageName, String fileName){ //fileName must include extension!!
		
		checkSDCard();
		if (available){
			File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() //root of the SD
						+ "/Android/data/" + packageName + "/files"); //inside "files" folder of the app
			path.mkdirs(); //make sure that folder exists
			
			file = new File(path, fileName);
		} 
	}
	
	public void write (String text){ //erases previous text and writes new text
		if(writeable){ // if it's possible to write on the SDCard
			try {
				if(!file.exists()){ 
					file.createNewFile(); //create file if it doesn't exist
				}
				FileWriter fileWriter = new FileWriter(file);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(text);
				bufferedWriter.close();
				
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		}
	}
	
	public void append (String text){ //adds text to the end of the file, does not erase
		if(writeable){ // if it's possible to write on the SDCard
			try {
				if(!file.exists()){ 
					file.createNewFile(); //create file if it doesn't exist
				}
				FileWriter fileWriter = new FileWriter(file,true); //will add to the end of the file
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(text);
				bufferedWriter.close();
				
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		}		
	}
	
	public void erase(){
		write("");
	}
	
	public void delete(){
		if(writeable){
			if (file.exists())
				file.delete();
		}
	}
	
	public String read(){
		String row = "";
		String content = "";
		
		if(available){ //if SDCard is available			
			if(file.exists()){ //if file exists
				try {
					FileInputStream fileInputStream = new FileInputStream(file);
					InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
					BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
					while ((row = bufferedReader.readLine()) != null) { //until the last line of the file
						content = content + row + "\n";    // content string will contain everything that is read
					}
					bufferedReader.close();
					inputStreamReader.close();
					fileInputStream.close();
		        } catch (Exception e) {
		            e.printStackTrace();
		        } 
			}
		}
		return content;   //if SDCard is not available or if
						 // file doesn't exist, an empty string will be returned   
	}
	
	public void checkSDCard(){
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    available = true;
		    writeable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    available = true;
		    writeable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    available = false;
		    writeable = false;
		}
	}
	
	public boolean isWriteable(){
		return writeable;
	}
	
	public boolean isAvailable(){
		return available;
	}
	
	public boolean fileExists(){
		return file.exists();
	}
	
}
